/*
 * @lc app=leetcode.cn id=213 lang=javascript
 *
 * [213] 打家劫舍 II
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var rob = function (nums) {
  const a = [...nums].slice(1);
  const b = [...nums].slice(0, nums.length - 1);
  var rob2 = function (nums) {
    let dp = [];
    dp[0] = nums[0];
    dp[1] = Math.max(nums[0], nums[1]);

    for (let n = 2; n < nums.length; n++) {
      dp[n] = Math.max(dp[n - 1], dp[n - 2] + nums[n]);
    }
    return dp[nums.length - 1];
  };
  return Math.max(rob2(a), rob2(b));
};

rob([2, 3, 4]);
// @lc code=end
